package net.garbage;

import java.io.*;
import java.net.Socket;

/**
 * @author Jose Martin Rozanec;
 */
public class NetPort extends Thread implements DataInput, DataOutput {
    private Socket socket;
    private DataOutputStream outstream;
    private DataInputStream instream;

    protected NetPort(Socket socket) throws IOException {
        this.socket = socket;
        instream = new DataInputStream(socket.getInputStream());
        outstream = new DataOutputStream(socket.getOutputStream());
    }

    public DataInputStream getInputStream() {
        return instream;
    }

    public DataOutputStream getOutputStream() {
        return outstream;
    }

    public void close() {
        try {
            instream.close();
            outstream.close();
            socket.close();
        }
        catch (IOException ioerror) {
            System.err.println("Error closing input stream.");
        }
    }

    protected void finalize() {
        close();
    }

    public void run() {
        while (true) {
            try {
                readInput();
            }
            catch (IOException ioerror) {
                System.err.println("Error reading input stream.");
                break;
            }
        }
    }

    private void readInput() throws IOException {
        //todo see concrete implementation
        processInput();
    }

    public void processInput() throws IOException {
        byte value = instream.readByte();
        System.out.println("ListingValueOn->NetPort_readInput: ." + value);
    }

    public void write(int byteval) throws IOException {
        outstream.write(byteval);
    }

    public void write(byte b[]) throws IOException {
        outstream.write(b);
    }

    public void write(byte b[], int off, int len) throws IOException {
        outstream.write(b, off, len);
    }

    public void writeBoolean(boolean v) throws IOException {
        writeBoolean(v);
    }

    public void writeByte(int v) throws IOException {
        outstream.writeByte(v);
    }

    public void writeShort(int v) throws IOException {
        outstream.writeShort(v);
    }

    public void writeChar(int v) throws IOException {
        outstream.writeChar(v);
    }

    public void writeInt(int v) throws IOException {
        outstream.writeInt(v);
    }

    public void writeLong(long v) throws IOException {
        outstream.writeLong(v);
    }

    public void writeFloat(float v) throws IOException {
        outstream.writeFloat(v);
    }

    public void writeDouble(double v) throws IOException {
        outstream.writeDouble(v);
    }

    public void writeBytes(String s) throws IOException {
        outstream.writeBytes(s);
    }

    public void writeChars(String s) throws IOException {
        outstream.writeChars(s);
    }

    public void writeUTF(String s) throws IOException {
        outstream.writeUTF(s);
    }

    public void readFully(byte b[]) throws IOException {
        instream.readFully(b);
    }

    public void readFully(byte b[], int off, int len) throws IOException {
        instream.readFully(b, off, len);
    }

    public int skipBytes(int n) throws IOException {
        return instream.skipBytes(n);
    }

    public boolean readBoolean() throws IOException {
        return instream.readBoolean();
    }

    public byte readByte() throws IOException {
        return instream.readByte();
    }

    public int readUnsignedByte() throws IOException {
        return instream.readUnsignedByte();
    }

    public short readShort() throws IOException {
        return instream.readShort();
    }

    public int readUnsignedShort() throws IOException {
        return instream.readUnsignedShort();
    }

    public char readChar() throws IOException {
        return instream.readChar();
    }

    public int readInt() throws IOException {
        return instream.readInt();
    }

    public long readLong() throws IOException {
        return instream.readLong();
    }

    public float readFloat() throws IOException {
        return instream.readFloat();
    }

    public double readDouble() throws IOException {
        return instream.readDouble();
    }

    public String readLine() throws IOException {
        return instream.readLine();
    }

    public String readUTF() throws IOException {
        return instream.readUTF();
    }

    public void flush() throws IOException {
        outstream.flush();
    }
}